<template>
  <div class="bottom-menu">
    <CheckButton
      class="select-all"
      :is-checked="isSelectAll"
      @click.native="checkClick"
    ></CheckButton>
    <span>全选</span>
    <span class="total-price">合计: ¥{{ totalPrice }}</span>
    <span class="buy-product" @click="calcClick"
      >去计算({{ checkLength }})</span
    >
  </div>
</template>

<script>
import CheckButton from "./CheckButton";
import { mapGetters } from "vuex";

export default {
  name: "BottomBar",
  components: {
    CheckButton
  },
  computed: {
    ...mapGetters(["cartList"]),

    totalPrice() {
      return this.cartList
        .filter(item => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return preValue + item.count * item.newPrice;
        }, 0)
        .toFixed(2);
    },
    checkLength() {
      return this.cartList.filter(item => item.checked).length;
    },
    isSelectAll() {
      //1.先判断购物车内没有商品
      if (this.cartList.length === 0) return false;
      //2.方法1 0返回true 不为0返回false
      //return !(this.cartList.filter(item => !item.checked).length)
      //3.性能更优找到第一个便不会再循环
      return !this.cartList.find(item => !item.checked);

      //4.传统方法
      // for (let item of this.cartList){
      //   if(!item.checked){
      //     return false
      //   }
      // }
      //return true;
    }
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) {
        //全部选中
        this.cartList.forEach(item => {
          item.checked = false;
        });
      } else {
        //部分或全部不选择
        this.cartList.forEach(item => {
          item.checked = true;
        });
      }
    },
    calcClick() {
      if (this.checkLength === 0) {
        this.$toast.showToast("请选购商品");
      }
    }
  }
};
</script>

<style scoped>
.bottom-menu {
  width: 100%;
  height: 44px;
  background-color: #eee;
  position: fixed;
  bottom: 50px;
  left: 0;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, 0.2);
  font-size: 14px;
  color: #888;
  line-height: 44px;
  padding-left: 35px;
  box-sizing: border-box;
}

.bottom-menu .select-all {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 13px;
}

.bottom-menu .total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.bottom-menu .buy-product {
  background-color: orangered;
  color: #fff;
  width: 100px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  float: right;
}
</style>
